package com.haoshuang.zhouzhoubang.ui.page.account.register

import androidx.lifecycle.rxLifeScope
import com.haoshuang.zhouzhoubang.model.api.ApiRepo
import com.haoshuang.zhouzhoubang.model.bean.UserBean
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseViewModel
import com.mufeng.libs.net.ResponseBean
import com.mufeng.libs.net.msg
import com.mufeng.libs.net.netRequest
import com.mufeng.libs.state.StateData
import com.mufeng.libs.state.StatefulMutableLiveData
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.onStart

class RegisterViewModel : BaseViewModel() {

    /**
     * 获取验证码
     */
    val sendSMSData = StatefulMutableLiveData<ResponseBean<Any>>()
    fun sendSMS(phone: String) = netRequest {
        start { sendSMSData.value = StateData.Loading }
        request { ApiRepo.sendSMS(phone, "1") }
        success { sendSMSData.value = StateData.Success(it) }
        error { code, msg ->
            sendSMSData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取备用手机号的验证码
     */
    val getSpareCodeData = StatefulMutableLiveData<ResponseBean<Any>>()
    fun getSpareCode(phone: String) = netRequest {
        start { getSpareCodeData.value = StateData.Loading }
        request { ApiRepo.sendSMS(phone, "1") }
        success { getSpareCodeData.value = StateData.Success(it) }
        error { code, msg ->
            getSpareCodeData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 注册
     */
    val registerData = StatefulMutableLiveData<ResponseBean<UserBean>>()
    fun register(
        phone: String,
        code: String,
        password: String,
        phoneSpare: String,
        codeSpare: String
    ) = netRequest {
        start { registerData.value = StateData.Loading }
        request { ApiRepo.register(phone, code, password, phoneSpare, codeSpare) }
        success { registerData.value = StateData.Success(it) }
        error { code, msg ->
            registerData.value = StateData.Error(code, msg)
        }
    }
}